//
//  HZL_StoryDetailPageViewController.m
//  B_Project
//
//  Created by 胡佳俊 on 15/11/28.
//  Copyright © 2015年 HZL. All rights reserved.
//

#import "HZL_StoryDetailPageViewController.h"
#import "HZL_StoryDetailTableViewCell.h"
@interface HZL_StoryDetailPageViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *modelDetailArr;
@property (nonatomic, retain)NSMutableDictionary *userDic;
@property (nonatomic, retain)NSMutableDictionary *detailDic;     //获取当前页文章信息总数据
@property (nonatomic, assign)CGFloat hTitleLable;  //记录第一个分区title的高度
@property (nonatomic, retain)HZL_RecommendModel *storyModel;

@end

@implementation HZL_StoryDetailPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"故事详情";
    
    [self getData];
    //创建tableView
    [self createTableView];
    [self loadMBProgress];
    [self addScreenEdgePan];
    
}
#pragma mark-滑动手势1
- (void)addScreenEdgePan
{
    UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(presentLeftMenuViewController:)];
    screenEdgePan.edges = UIRectEdgeLeft;
    [self.tableView addGestureRecognizer:screenEdgePan];
    
}
#pragma mark-滑动手势2
-(void)presentLeftMenuViewController:(UIScreenEdgePanGestureRecognizer *)edge
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 获取数据
- (void)getData
{
    NSString *url = [NSString stringWithFormat:@"%@%@", @"http://api.breadtrip.com/v2/new_trip/spot/?spot_id=",self.spot_id];
    [HZL_NetworkingHandle getUrl:url cookie:nil result:^(id result) {
        //用来接收result
        NSMutableDictionary *dic = result;
        //获取当前页文章信息总数据
        self.detailDic = dic[@"data"][@"spot"];
        //获取当前页用户信息数据
        self.userDic = dic[@"data"][@"trip"][@"user"];
        
        
        //将文章信息数据转化为对象
        self.modelDetailArr = [HZL_RecommendModel transformWithArray:self.detailDic[@"detail_list"]];
        
        [self.hud removeFromSuperview];
        [self.tableView reloadData];
        
    }];
}

#pragma mark - 创建tableView
- (void)createTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT) style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle =    UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    
}


#pragma mark - tableView的分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //一个分区保存title 一个分区保存图和字的数据
    return 2;
}

#pragma mark - 头部行高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return VIEW_WIDTH / 3;
    }
    else{
        return 0;
    }
    
}

#pragma mark - header上面的view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 100)];
        view.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
        
        
        //头像
        UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(30, 30, VIEW_WIDTH / 6, VIEW_WIDTH / 6)];
        img.layer.masksToBounds = YES;
        img.layer.cornerRadius = img.frame.size.width / 2;
        [view addSubview:img];
        [img sd_setImageWithURL:[NSURL URLWithString:self.userDic[@"avatar_m"]] placeholderImage:[UIImage sd_animatedGIFNamed:@"load"]];
        
        //文字
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(img.frame.origin.x + img.frame.size.width + 10, img.frame.origin.y + 20, VIEW_WIDTH - 30- img.frame.size.width - 20 - 20, 30)];
        [view addSubview:label];
        label.font = [UIFont fontWithName:@" Menlo-Italic" size:15];
        label.text = self.userDic[@"name"];
        
        //横线label
        UILabel *labelLine = [[UILabel alloc]initWithFrame:CGRectMake(20, img.frame.origin.y + img.frame.size.height + 10, VIEW_WIDTH - 60, 1)];
        labelLine.backgroundColor = [UIColor darkGrayColor];
        [view addSubview:labelLine];
        
        
        //夜间模式
        if ([[NSUserDefaults standardUserDefaults]boolForKey:@"isNight"]) {
            view.backgroundColor = [UIColor colorWithRed:0.28 green:0.28 blue:0.28 alpha:1];
            label.textColor = [UIColor whiteColor];
            labelLine.backgroundColor = [UIColor whiteColor];
        }else
        {
            view.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
        }
        
        return view;
    }
    else
    {
        return nil;
    }
    
}

#pragma mark - footer的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    }
    else
    {
        return VIEW_WIDTH / 3 - 50;
    }
}

#pragma mark - tableViewfooterView
//时间view
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return nil;
    }
    else{
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_WIDTH / 3 - 50)];
        view.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
        
        //横线label
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(30, 0, VIEW_WIDTH - 60, 1)];
        label.backgroundColor = [UIColor darkGrayColor];
        [view addSubview:label];
        
        //时间图片
        UIImageView *img = [[UIImageView alloc]init];
        img.frame = CGRectMake(30, view.frame.size.height / 2 - 10, 20, 20);
        img.image = [UIImage imageNamed:@"time"];
        [view addSubview:img];
        
        //时间
        UILabel *lable = [[UILabel alloc]init];
        label.font = [UIFont systemFontOfSize:13];
        lable.frame = CGRectMake(img.frame.origin.x + 20 + 10, img.frame.origin.y, 300, img.frame.size.height);
        lable.textColor = [UIColor darkGrayColor];
        
        
        NSString *str = [self.detailDic[@"date_tour"]  substringToIndex:19];
        NSString *str1 = [str stringByReplacingOccurrencesOfString:@"T" withString:@"  "];
        lable.text = str1;
        [view addSubview:lable];
        
        
        view.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
        
        
        return view;
    }
    
}

#pragma mark - tableViewc cell的count
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    else{
        return self.modelDetailArr.count;
    }
}
#pragma mark - tableView cell的数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell =   [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

        cell.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
        
        cell.textLabel.text = self.detailDic[@"text"];
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.textColor = [UIColor darkGrayColor];
        cell.textLabel.font = [UIFont systemFontOfSize:17];
        self.hTitleLable = [[self class]heightWithText:cell.textLabel.text fontSize:17 Width:VIEW_WIDTH - 40];
        
        cell.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
        return cell;
    }
    else
    {
        HZL_StoryDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"storyDetail"];
        if (!cell) {
            cell = [[HZL_StoryDetailTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"storyDetail"];
        }
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
        cell.detailModel = self.modelDetailArr[indexPath.row ];
        cell.userDic = self.userDic;
        return cell;
    }
}


#pragma mark - tableView行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //0分区 返回文章标题
    if (indexPath.section == 0) {
        return self.hTitleLable ;
    }
    else{
        
        HZL_RecommendModel *tempModel = self.modelDetailArr[indexPath.row];
        CGFloat h = [[self class]heightWithText:tempModel.text fontSize:15 Width:VIEW_WIDTH - 40];
        
    
        return (tempModel.photo_height).integerValue * VIEW_WIDTH / (tempModel.photo_width).integerValue + h + 10;
        
    }
}

#pragma mark - tableView点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
